import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;


public class Ex4_4 {

	public Ex4_4() {}

	public static void main(String[] args) {
		Ex4_4 exe = new Ex4_4();
		ArrayList<String> dnas = 
				exe.getDNAs("C:\\Users\\josemfer\\Downloads\\DNAs.txt");
		exe.deBruijn(dnas);
	}


	
	
	private void deBruijn(ArrayList<String>  reads) {
		HashMap<String,ArrayList<String>> graph = 
							new HashMap<String,ArrayList<String>>();
		for (int i=0;i<reads.size() ;i++) {
			String text = reads.get(i);
			String kmer = text.substring(0,text.length()-1);
			String nkmer = text.substring(1,text.length());
			ArrayList<String> list = graph.get(kmer);
			if (list == null) {
				ArrayList<String> newList = new ArrayList<String>();
				newList.add(nkmer);
				graph.put(kmer, newList);
			} else {
				if (!list.contains(nkmer)) {
					list.add(nkmer);
				}
			}			
		}
		
		String result = paintMap(graph);
		writeFile("C:\\Users\\josemfer\\Downloads\\ex4_2_result.txt",result);
		
	}
	
	
	
	
	/*
	private void deBruijn(ArrayList<String>  kmers) {
		HashMap<String, ArrayList<String>> hm = 
				new HashMap<String, ArrayList<String>>();		
		String result = "";
		for (int i=0;i<kmers.size();i++) {
			String kmer = kmers.get(i);
			String prefix = kmer.substring(0,kmer.length()-1);
			String sufix = kmer.substring(1,kmer.length());
			for (int j=i+1;j<kmers.size();j++) {
				String kmer2 = kmers.get(j);
				String prefix2 = kmer2.substring(0,kmer2.length()-1);
				String sufix2 = kmer2.substring(1,kmer2.length());

				
				if (prefix.equals(sufix2)) {
					ArrayList<String> list = hm.get(sufix2);
					if (list == null) {
						ArrayList<String> newList = new ArrayList<String>();
						newList.add(prefix);
						hm.put(sufix2, newList);
					} else {
						if (!list.contains(prefix)) {
							list.add(prefix);
						}
					}						
				}

				if (sufix.equals(prefix2)) {
					ArrayList<String> list = hm.get(sufix);
					if (list == null) {
						ArrayList<String> newList = new ArrayList<String>();
						newList.add(prefix2);
						hm.put(sufix, newList);
					} else {
						if (!list.contains(prefix2)) {
							list.add(prefix2);
						}
					}						
				}			
			}
		}
	
		
		result = paintMap(hm);
		writeFile("C:\\Users\\josemfer\\Downloads\\ex4_2_result.txt",result);

	} */


	private String paintMap(HashMap<String, ArrayList<String>> graph) {		
		String res = "";		
		
		SortedSet<String> keys = new TreeSet<String>(graph.keySet());
		
		for(String kmer : keys) {			
			ArrayList<String> list =  (ArrayList<String>) graph.get(kmer);			
			int i = 0;
			res += kmer + " -> ";
			for (String km : list) {				
				if (i > 0) {					
					res+= ",";
				}				
				res += km;
				i++;
			}				
			res += "\n";
		}


		return res;


	}


	private void writeFile(String path, String text) {
		try
		{
			String file_name = path;
			FileWriter file = new FileWriter(file_name);
			BufferedWriter out = new BufferedWriter (file);			
			out.write(text);
			out.close();
		}
		catch (IOException e)
		{}
	}

	private ArrayList<String> getDNAs(String file) {		
		ArrayList<String> lineas = new ArrayList<String>();		
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				lineas.add(line);
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return lineas;		
	}


}
